<template>
  <el-card>
    <div slot="header">
      <span>{{ model.title.value }}</span>
    </div>
    <div
      :ref="model.id.value"
      :style="{height: model.height.value}"
    ></div>
  </el-card>
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'Gauge',
  props: ['model'],
  data() {
    return {
      chart: '',
      option: {
        tooltip: {
          formatter: '{a} <br/>{b} : {c}%'
        },
        series: [
          {
            name: 'Pressure',
            type: 'gauge',
            progress: {
              show: true
            },
            detail: {
              valueAnimation: true,
              formatter: '{value}'
            },
            data: [
              {
                value: 50,
                name: 'SCORE'
              }
            ]
          }
        ]
      }
    }
  },
  watch: {
    model: {
      deep: true,
      handler(val) {
        this.onResize()
      }
    }
  },
  mounted() {
    this.handleQuery()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
  },
  methods: {
    handleQuery() {
      const chartDom = this.$refs[this.model.id.value]
      this.chart = echarts.init(chartDom)
      this.chart.setOption(this.option)
    },
    onResize() {
      this.chart.resize()
    }
  }
}
</script>

<style scoped>

</style>
